package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.*;
import com.gxa.p2p.pojo.*;
import com.gxa.p2p.service.BidService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service("bidService")
@Transactional
public class BidServiceImpl implements BidService {
    @Resource
    BidMapper bidMapper; // 投标记录
    @Resource
    AccountInfoMapper accountInfoMapper; // 用户信息
    @Resource
    AssetDetailsMapper assetDetailsMapper; // 账户流水
    @Resource
    InvestRecordMapper investRecordMapper; // 记录
    @Resource
    private LoanBidsDetailMapper loanBidsDetailMapper; // 标的详情

    @Override
    public boolean saveBid(Bid bid) {
        int sum = 0;
        // 传入标的详情，根据标的详情  记录投资记录到投标标，并计算出标的投资状态，记录到投资bid表/invest_record
        sum+=bidMapper.insertSelective(bid);
        // 更新用户信息-----------------------
        AccountInfo accountInfo = accountInfoMapper.selectByUserId(bid.getUserId());
        //计算年利息
        BigDecimal multiply = new BigDecimal(bid.getActualRate() + "").multiply(new BigDecimal(bid.getAvailableAmount() + ""));
        // 计算本息
        BigDecimal add = multiply.add(new BigDecimal(bid.getAvailableAmount() + ""));
        // 设置待收利息
        accountInfo.setUnreceiveInterest(accountInfo.getUnreceiveInterest().add(multiply));
        // 设置待收本金
        accountInfo.setUnreceivePrincipal(accountInfo.getUnreceivePrincipal().add(
                new BigDecimal(bid.getAvailableAmount() + "")
        ));
        // 设置冻结金额
        accountInfo.setFrozenAmount(accountInfo.getFrozenAmount().add(add));
        // 更新
        sum += accountInfoMapper.updateByPrimaryKeySelective(accountInfo);

        // 账户流水------------------------
        AssetDetails assetDetails = assetDetailsMapper.selectByPrimaryKey(bid.getUserId());
        System.out.println("0000000000000000000  "+assetDetails);
        // 设置代收本金
        assetDetails.setUnreceivePrincipal(assetDetails.getUnreceivePrincipal().add(new BigDecimal(bid.getAvailableAmount() + "")));
        // 设置支出
        assetDetails.setExpense(assetDetails.getExpense().add(new BigDecimal(bid.getAvailableAmount() + "")));
        // 设置冻结金额
        assetDetails.setFrozenAmount(assetDetails.getFrozenAmount().add(add));
        // 更新
        sum += assetDetailsMapper.updateByPrimaryKeySelective(assetDetails);

        // 标的详情表中相关字段-----------------------------------------------
        LoanBidsDetail loanBidsDetail = loanBidsDetailMapper.selectByPrimaryKey(bid.getBidRequestId());
        // 计算标的募集进度
        double TotalAmount = (int) (loanBidsDetail.getTotalAmount() * 1000);
        double RaiseFund=0;
        // 判断标是否是第一次被投，若不是直接在原有进度上增加（已更改数据库默认值为0.00）
        if (loanBidsDetail.getRaiseFund() != null){
             RaiseFund += ((int) (loanBidsDetail.getRaiseFund() * 1000));
        }
//        double RaiseFund = (int) (loanBidsDetail.getRaiseFund() * 1000 );

        if (TotalAmount == RaiseFund) {
            // 根据进度设置标的状态
            loanBidsDetail.setStatus(4);
        } else {
            // 计算招标金额
            RaiseFund= ((int) (RaiseFund + bid.getAvailableAmount() * 1000) )/ 1000.00;
            // 设置招标金额
            loanBidsDetail.setRaiseFund(RaiseFund);
            // 计算并设置进度
            int Ra = (int) (RaiseFund * 1000);
            System.out.println("+++++++++++++++ "+Ra / TotalAmount);
            Double inv = ((Ra / TotalAmount) * 10000)/ 10000.00;
            loanBidsDetail.setInvestmentProgress(inv);
        }
        // 更新标的信息
        System.out.println("-------执行成功----------");
        sum += loanBidsDetailMapper.updateByPrimaryKeySelective(loanBidsDetail);
        // 操作invest_record表----------------------------------------------
        InvestRecord investRecord = new InvestRecord();
        investRecord.setUserId(bid.getUserId());
        investRecord.setBidId(bid.getBidRequestId());
        investRecord.setInvestAmount(bid.getAvailableAmount()+"");
        investRecord.setRate(Float.valueOf(bid.getActualRate()+""));
        investRecord.setRateManageCost(loanBidsDetail.getManageFeeMonthlyRate());
        investRecord.setUnreceivedAmount(add.doubleValue());
        sum+=investRecordMapper.insertSelective(investRecord);
        System.out.println("++++++++hhhhhhhhhhhh++++++++++: "+sum);

        return sum >= 5;
    }

    @Override
    public List<Bid> findUserAll(Integer user_id) {
        // 查询用户投资详情

        return null;
    }

}
